﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;

namespace YJKGridTopologyCal.Geometries.Utilities
{
	public class PolygonExtracter : IGeometryFilter
	{
		public static IList<IGeometry> GetPolygons(IGeometry geom, IList<IGeometry> list)
		{
			if (geom is IPolygon)
			{
				list.Add(geom);
			}
			else if (geom is IGeometryCollection)
			{
				geom.Apply(new PolygonExtracter(list));
			}
			return list;
		}

		public static IList<IGeometry> GetPolygons(IGeometry geom)
		{
			return PolygonExtracter.GetPolygons(geom, new List<IGeometry>());
		}

		public PolygonExtracter(IList<IGeometry> comps)
		{
			this._comps = comps;
		}

		public void Filter(IGeometry geom)
		{
			if (geom is IPolygon)
			{
				this._comps.Add(geom);
			}
		}

		private readonly IList<IGeometry> _comps;
	}
}
